//
//  WeatherView.m
//  SiShi
//
//  Created by whbalzac on 09/10/2017.
//  Copyright © 2017 whbalzac. All rights reserved.
//

#import "WHWeatherView.h"
#import "WHWeatherSunView.h"
#import "WHWeatherClound.h"
#import "WHWeatherRainView.h"
#import "WHWeatherSnowView.h"
#import "WHWeatherHeader.h"

#define kWeatherChangeAnimationDuration 1.0

@interface WHWeatherView ()
@property (nonatomic, strong) WHWeatherBaseView *displayView;
@property (nonatomic, strong) WHWeatherBaseView *willDisplayView;
@property (nonatomic, strong) NSMutableArray *animationArray;
@end

@implementation WHWeatherView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self configueWeather];
    }
    return self;
}

- (void)configueWeather
{
    [self showWeatherAnimationWithType:WHWeatherTypeSun];
}

#pragma mark -
#pragma mark - Priavte Method

- (void)showWeatherAnimationWithType:(WHWeatherType)weatherType {
    [self addOrRemoveAnimationQueueWithType:NO addType:weatherType];
}

- (void)addOrRemoveAnimationQueueWithType:(BOOL)isRemove addType:(WHWeatherType)weatherType {
    if (self.animationArray) {
        if (isRemove) {
            if (self.animationArray.count > 0) {
                [self.animationArray removeObjectAtIndex:0];
                
                if (self.animationArray.count > 0) {
                    [self startAnimationQueue];
                }
            }
        } else {
            if (self.animationArray.count == 0) {
                [self.animationArray addObject:@(weatherType)];
                [self startAnimationQueue];
            } else {
                [self.animationArray addObject:@(weatherType)];
            }
        }
    }
}

- (void)startAnimationQueue
{
    if (!self.animationArray || !self.animationArray.count) {
        return;
    }
    
    NSNumber *nextAnimationNumber = self.animationArray[0];
    WHWeatherType weatherType = nextAnimationNumber.integerValue;
    
    if (weatherType == WHWeatherTypeSun || weatherType == WHWeatherTypeOther) {

        WHWeatherSunView *sunView = [[WHWeatherSunView alloc] init];
        sunView.frame = self.frame;
        [self addSubview:sunView];
        self.willDisplayView = sunView;
    }else if (weatherType == WHWeatherTypeClound) {
        
        WHWeatherClound *cloundView = [[WHWeatherClound alloc] init];
        cloundView.frame = self.frame;
        [self addSubview:cloundView];
        self.willDisplayView = cloundView;
    }else if (weatherType == WHWeatherTypeRain) {
        
        WHWeatherRainView *rainView = [[WHWeatherRainView alloc] init];
        rainView.isLighting = NO;
        rainView.frame = self.frame;
        [self addSubview:rainView];
        self.willDisplayView = rainView;
    }else if (weatherType == WHWeatherTypeRainLighting) {
        
        WHWeatherRainView *rainView = [[WHWeatherRainView alloc] init];
        rainView.isLighting = YES;
        rainView.frame = self.frame;
        [self addSubview:rainView];
        self.willDisplayView = rainView;
    }else if (weatherType == WHWeatherTypeSnow) {
        
        WHWeatherSnowView *snowView = [[WHWeatherSnowView alloc] init];
        snowView.frame = self.frame;
        [self addSubview:snowView];
        self.willDisplayView = snowView;
    }
    
    [UIView animateWithDuration:kWeatherChangeAnimationDuration
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         if (self.willDisplayView) {
                             [self.willDisplayView startAnimation];
                         }
                         
                         if (self.displayView) {
                             [self.displayView stopAnimation];
                         }
                         
                     } completion:^(BOOL finished) {
//                         if (finished) {
                             [self.displayView removeFromSuperview];
                             self.displayView = nil;
                             self.displayView = self.willDisplayView;
                             [self addOrRemoveAnimationQueueWithType:YES addType:0];
//                         }
                     }];
}

#pragma mark -
#pragma mark - Getter
- (NSMutableArray *)animationArray
{
    if (!_animationArray) {
        _animationArray = [NSMutableArray array];
    }
    return _animationArray;
}

- (UIImageView *)weatherBackImageView
{
    if (!_weatherBackImageView) {
        _weatherBackImageView = [[UIImageView alloc] init];
        _weatherBackImageView.userInteractionEnabled = YES;
        _weatherBackImageView.clipsToBounds = YES;
        _weatherBackImageView.image = [UIImage imageNamed:@"sunBg.jpg"];
    }
    return _weatherBackImageView;
}
@end
